package com.ctshk.rpc.train.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.rpc.train.dto.TrainProductManagePageDTO;
import com.ctshk.rpc.train.dto.TrainProductStrategyDTO;
import com.ctshk.rpc.train.entity.TrainProductStrategy;
import com.ctshk.rpc.train.req.TrainProductPageReq;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 火车票产品加幅策略 Mapper 接口
 * </p>
 *
 * @author 彭柳霖
 * @since 2021-03-15
 */
public interface TrainProductStrategyMapper extends BaseMapper<TrainProductStrategy> {

    /**
     * 产品中心-获取火车票产品分页列表
     * @param req
     * @return
     */
    @Select("<script>"+"SELECT b.id,b.departure_city_name,b.arrive_city_name,\n" +
            "b.seat_level_name,b.train_number_name,b.railway_corporation_name," +
            "b.departure_time,b.boarding_station_name,b.product_source, \n" +
            "b.alighting_station_name,\n" +
            "b.supplier_name,b.intended_for,b.pack_status,b.on_shelf_status,b.recommendation_status, \n" +
            "b.sale_status,b.intended_for_name,b.product_code,b.terminus_time,b.charge_person_name\n" +
            "from t_train_product b \n" +
            "left join t_train_product_strategy a on b.id  = a.train_product_id \n" +
            "WHERE b.is_deleted = 0 <if test=\"req.railwayCorporationName != null and req.railwayCorporationName != ''\">AND b.railway_corporation_name LIKE CONCAT('%',#{req.railwayCorporationName},'%')</if> \n" +
            "<if test=\"req.trainNumberName != null and req.trainNumberName != ''\">AND b.train_number_name LIKE CONCAT('%',#{req.trainNumberName},'%')</if> \n" +
            "<if test=\"req.departureCityName != null and req.departureCityName != ''\">AND b.departure_city_name LIKE CONCAT('%',#{req.departureCityName},'%')</if> \n" +
            "<if test=\"req.arriveCityName != null and req.arriveCityName != ''\">AND b.arrive_city_name LIKE CONCAT('%',#{req.arriveCityName},'%')</if> \n" +
            "<if test=\"req.boardingStationName != null and req.boardingStationName != ''\">AND b.boarding_station_name LIKE CONCAT('%',#{req.boardingStationName},'%')</if> \n" +
            "<if test=\"req.alightingStationName != null and req.alightingStationName != ''\">AND b.alighting_station_name LIKE CONCAT('%',#{req.alightingStationName},'%')</if> \n" +
            "<if test=\"req.productSource != null and req.productSource != 0\">AND b.product_source = #{req.productSource}</if> \n" +
            "<if test=\"req.recommendationStatus != null and req.recommendationStatus != 0\">AND b.recommendation_status = #{req.recommendationStatus}</if> \n" +
            "<if test=\"req.supplierName != null and req.supplierName != ''\">AND b.supplier_name LIKE CONCAT('%',#{req.supplierName},'%')</if> \n" +
            "<if test=\"req.saleStatus != null and req.saleStatus != 0\">AND b.sale_status = #{req.saleStatus}</if> \n" +
            "<if test=\"req.dataPermissionUserIds != null\">AND (" +
            "b.create_id in " +
            "<foreach collection='req.dataPermissionUserIds' item='id' open='(' separator=',' close=')'>"+
            "#{id}"+
            "</foreach>"+
            "or b.charge_person_id in " +
            "<foreach collection='req.dataPermissionUserIds' item='id' open='(' separator=',' close=')'>"+
            "#{id}"+
            "</foreach> )</if> \n"+
            "<if test=\"req.channelType != null and req.channelType == 1\">" +
            "and a.id in  ( \n"+
            "select increase_strategy_id from t_train_product_channel_online_self_platform   \n"+
            "union select increase_strategy_id from  t_train_product_channel_online_third_platform"+
            ") \n"+
            "</if> \n" +

            "<if test=\"req.channelType != null and req.channelType == 2\">" +
            "and a.id in  ( \n"+
            "select increase_strategy_id from t_train_product_channel_online_third_platform"+
            ") \n"+
            "</if> \n" +

            "<if test=\"req.channelType != null and req.channelType == 3\">" +
            "and a.id in  ( \n"+
            "select increase_strategy_id from t_train_product_channel_online_self_platform"+
            ") \n"+
            "</if> \n" +

            "<if test=\"req.channelType != null and req.channelType == 4\">" +
            "and a.id in  ( \n"+
            "select increase_strategy_id from t_train_product_channel_offline_self_custom   \n"+
            "union select increase_strategy_id from  t_train_product_channel_offline_agent_custom"+
            ") \n"+
            "</if> \n" +

            "<if test=\"req.channelType != null and req.channelType == 5\">" +
            "and a.id in  ( \n"+
            "select increase_strategy_id from t_train_product_channel_offline_self_custom"+
            ") \n"+
            "</if> \n" +

            "<if test=\"req.channelType != null and req.channelType == 6\">" +
            "and a.id in  ( \n"+
            "select increase_strategy_id from t_train_product_channel_offline_agent_custom"+
            ") \n"+
            "</if> \n" +

            "GROUP BY b.id order by id desc "+"</script>")
    IPage<TrainProductManagePageDTO> queryList(@Param("req") TrainProductPageReq req, IPage<TrainProductManagePageDTO> agentPage);



    @Select({"<script>",
            "select id from t_train_product_strategy where 1=1 ",
            "and train_product_id in <foreach collection='list' item='item' open='(' close=')' separator=','>",
            "#{item}",
            "</foreach>",
            "</script>"})
    List<Long> selectIdListByTrainProductIds(List<Long> trainProductIds);


    /**
     * 根据产品id和日期查询策略
     */
    @Select("<script>"+"select a.id,a.increase_strategy_name," +
            "a.self_sale_permission_type," +
            "a.agent_sale_permission_type,a.self_sale_permission_id," +
            "a.agent_sale_permission_id " +
            "from t_train_product_strategy a \n" +
            "left join t_train_product b on a.train_product_id  = b.id \n" +
            "left join t_train_product_strategy_date c on a.id = c.increase_strategy_id \n" +
            "WHERE b.is_deleted = 0 \n" +
            "AND c.date_begin &lt;=#{queryDate} and c.date_end &gt;=#{queryDate}\n" +
            "AND b.id =#{productId} \n" +
            "group by b.id limit 1\n" +
            "</script>")
    TrainProductStrategyDTO queryTrainProductStrategyInfo(@Param("productId") Long productId, @Param("queryDate") LocalDate queryDate);

    /**
     * 根据产品id和日期查询策略
     */
    @Select("<script>"+"select a.id,a.increase_strategy_name," +
            "a.self_sale_permission_type," +
            "a.agent_sale_permission_type,a.self_sale_permission_id," +
            "a.agent_sale_permission_id " +
            "from t_train_product_strategy a \n" +
            "join t_train_product b on a.train_product_id  = b.id \n" +
            "join t_train_product_strategy_date c on a.id = c.increase_strategy_id \n" +
            "WHERE b.is_deleted = 0 \n" +
            "AND c.date_begin &lt;=#{queryDate} and c.date_end &gt;=#{queryDate}\n" +
            "AND b.id =#{productId} \n" +
            "group by b.id limit 1\n" +
            "</script>")
   TrainProductStrategy findTrainProductStrategy(@Param("productId") Long productId,@Param("queryDate") LocalDate queryDate);
}
